﻿using IdentityModel.Client;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Models.Identity;
using Models.JsonResultModel;
using Models.WebClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http.Headers;
using System.Web;
using UtilsBase;
using UtilsCache;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;

namespace ServiceWeb.WebClient
{
    public class ServiceClientLoginToken
    {
        public TokenResponse getToken(string account, string pwd)
        {
            // pwd = new UtilsStringQuery().DoMd5Encode(pwd, "32");
            var client = new HttpClient();
            var tokenResponse = client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address = UtilAppsettingCache.getSetting("authorize:Address"),
                ClientId = "client",
                ClientSecret = "secret",
                GrantType = OpenIdConnectGrantTypes.Password,
                UserName = account,
                Password = pwd,

            }); ;
            return tokenResponse.Result;
        }
        public TokenResponse getRefreshToken(string refreshToken)
        {
            var client = new HttpClient();
            var tokenResponse = client.RequestRefreshTokenAsync(new RefreshTokenRequest
            {
                ClientId = "client",
                RefreshToken = refreshToken,
                GrantType = OpenIdConnectGrantTypes.RefreshToken,
                ClientSecret = "secret",
                Address = UtilAppsettingCache.getSetting("authorize:Address"),
            });
            return tokenResponse.Result;
        }

        public ClientLoginUserInfo getUserInfo(string token)
        {
            var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            var response = client.GetAsync(UtilAppsettingCache.getSetting("authorize:getUserInfo"));  //需要请求接口/controller的地址
            var result = response.Result;//在这里会等待task返回。
            ClientLoginUserInfo clientLoginUserInfo = new ClientLoginUserInfo();
            if (result.IsSuccessStatusCode)
            {
                string json = result.Content.ReadAsStringAsync().Result;
                if (json != null)
                {
                    //IdentityServerUser identityUser = Newtonsoft.Json.JsonConvert.DeserializeObject<IdentityServerUser>(json);
                    //if (identityUser != null)
                    //{
                    //    rm_client_user iot_Admin_User = new UtilsORM.EFIotRemoteManageContext().rm_client_user.Where(c => c.cu_guid == identityUser.sub).FirstOrDefault();
                    //    clientLoginUserInfo.userGuid = identityUser.sub;
                    //    clientLoginUserInfo.userName = iot_Admin_User.cu_name;
                    //    clientLoginUserInfo.account = iot_Admin_User.cu_account;
                    //}
                }
            }
            return clientLoginUserInfo;
        }
    }
}